#ifndef CPP_DISPLAY_H
#define CPP_DISPLAY_H

#include <Global.h>
#include <MiniGui.h>
#include <Rect.h>

const float PI = 3.14159265;

//@brief 设备类,设备相关信息获取，变换
//@author cvte
class Display {

    public:
        static void setDesignResolution(int designWidth, int designHeight, int screenRotation) {
            Display::designWidth = designWidth;
            Display::designHeight = designHeight;
            Display::screenRotation = screenRotation;
        }

        static int getDesignWidth() {
            return Display::designWidth;
        }

        static int getDesignHeight() {
            return Display::designHeight;
        }

        static int getScreenRotation() {
            return Display::screenRotation;
        }

        //@method 函数简要说明-，宽变换函数
        //@param width 设计宽
        //@param height 设计高 @see CTest
        //@return 返回说明 变换宽
        static int transformWidth(int width, int height) {
            float radian = screenRotation*PI/180;
            return abs((int)(width * cos(radian)) - (int)(height * sin(radian)));
        }

        static int transformHeight(int width, int height) {
            float radian = screenRotation*PI/180;
            return abs((int)(width * sin(radian)) + (int)(height * cos(radian)));
        }

        //@method 变换Ｙ
        //@param x: x坐标
        //@param y: y坐标
        //@param width: 宽度
        //@param height: 高度
        //@param designW: 设计宽
        //@param designH: 设计高
        static int transformX(int x, int y, int width, int height, int designW, int designH) {
            switch (screenRotation) {
                case 0: {
                    return x;
                } break;
                case 90: {
                    return designH - y - height;
                } break;
                case 180: {
                    return designW - x - width;
                } break;
                case 270: {
                    return y;
                } break;
            }
            return x;
        }

        static int transformY(int x, int y, int width, int height, int designW, int designH) {
            switch (screenRotation) {
                case 0: {
                    return y;
                } break;
                case 90: {
                    return x;
                } break;
                case 180: {
                    return designH - height - y;
                } break;
                case 270: {
                    return designW - x - width;
                } break;
            }
            return y;

            //float radian = screenRotation*PI/180;
            //if (screenRotation == 270) {
            //    return transformHeight(designW, designH) - transformHeight(width, height) - abs((int)(x * sin(radian)) + (int)(y * cos(radian)));
            //}
            //return abs((int)(x * sin(radian)) + (int)(y * cos(radian)));
        }

        //从物理坐标X转换为设计坐标X
        static int transformToDesignX(int x, int y, int width, int height, int designW, int designH) {
            switch (screenRotation) {
                case 0: {
                    return x;
                } break;
                case 90: {
                    return y;
                } break;
                case 180: {
                    return designW - x - width;
                } break;
                case 270: {
                    return designW - y - height;
                } break;
            }
            return x;
        }
        //从物理坐标Y转换为设计坐标Y
        static int transformToDesignY(int x, int y, int width, int height, int designW, int designH) {
            switch (screenRotation) {
                case 0: {
                    return y;
                } break;
                case 90: {
                    return designH - x - width;
                } break;
                case 180: {
                    return designH - height - y;
                } break;
                case 270: {
                    return x;
                } break;
            }
            return y;
        }

        static RECT transformRect(Rect& rect, int designW, int designH) {
            int rectX = rect.getLeft();
            int rectY = rect.getTop();
            int rectW = rect.getRight() - rect.getLeft();
            int rectH = rect.getBottom() - rect.getTop();

            int transX = Display::transformX(rectX, rectY, rectW, rectH, designW, designH);
            int transY = Display::transformY(rectX, rectY, rectW, rectH, designW, designH);
            int transW = Display::transformWidth(rectW, rectH);
            int transH = Display::transformHeight(rectW, rectH);

            RECT text_rect;
            text_rect.left = transX;
            text_rect.top = transY;
            text_rect.right = text_rect.left + transW;
            text_rect.bottom = text_rect.top + transH;

            return text_rect;
        }

        static Rect transformRect(Rect& parentRect, Rect& childRect) {
            int rectX = childRect.getLeft();
            int rectY = childRect.getTop();
            int rectW = childRect.getWidth();
            int rectH = childRect.getHeight();

            int designW = parentRect.getWidth();
            int designH = parentRect.getHeight();

            int transX = Display::transformX(rectX, rectY, rectW, rectH, designW, designH);
            int transY = Display::transformY(rectX, rectY, rectW, rectH, designW, designH);
            int transW = Display::transformWidth(rectW, rectH);
            int transH = Display::transformHeight(rectW, rectH);

            Rect result(transX, transY, transX + transW, transY + transH);
            return result;
        }

    private:
        //屏幕旋转角度
        static int screenRotation;
        //设计稿宽度
        static int designWidth;
        //设计稿高度
        static int designHeight;
};

#endif /* CPP_DISPLAY_H */